/*
 * @Author: wuqi
 * @Date: 2023-11-14 15:29:01
 * @Description:
 */

/**
 * @description: 用来下载jpg,png图片的方法
 * @param {String} link
 * @param {String} picName
 */
export function download(link: string, picName: string = "ai.png") {
  let img = new Image();
  img.setAttribute("crossOrigin", "Anonymous");
  img.onload = function () {
    let canvas = document.createElement("canvas");
    let context = canvas.getContext("2d");
    canvas.width = img.width;
    canvas.height = img.height;
    context?.drawImage(img, 0, 0, img.width, img.height);
    canvas.toBlob((blob:any) => {
      let url = URL.createObjectURL(blob);
      let a = document.createElement("a");
      let event = new MouseEvent("click");
      a.download = picName;
      a.href = url;
      a.dispatchEvent(event);
      URL.revokeObjectURL(url); // 内存管理,将这句代码注释掉,则将以 blob:http 开头的url复制到浏览器地址栏有效,否则无效.
    });
  };
  img.src = link + "?v=" + Date.now();
}
